﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Common;
using PM.Entity;
using System.Collections.Generic;

namespace PM.Site
{
    public partial class UsersForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }

            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
           

        }

        private void Loading()
        {
            
            if (!Page.IsPostBack)
            {
                UserAccountCollection uac = UserAccountServices.GetUserAccountCollection();
                Session["UserAccounts"] = uac;
                dlUserAccounts.DataSource = uac;
                dlUserAccounts.DataBind();
                ddlUserTypes.DataSource = UserTypeServices.GetUserTypeCollection();
                ddlUserTypes.DataBind();
                ddlClientIds.DataSource = ClientServices.GetClientID_Name_Collection();
                ddlClientIds.DataBind();
                
            }
            
        }



        protected void dlUserAccounts_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string userName = e.CommandArgument.ToString();
            switch (e.CommandName)
            {
                case "DeleteUserAccount":
                    UserAccountServices.Delete(userName);
                    Response.Redirect("~/UsersForm.aspx");
                    break;
                /*case "EditUserAccount":
                    this.LoadUserAccountByUserName(userName);                    
                    break;*/
            }

        }

      

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txbUserName.Text == "" || txbPassword.Text == "" || UserAccountServices.CheckExist(txbUserName.Text))
            {              
                return;
            }

            String sHashedPass = MD5Calculator.CalculateMD5Hash(txbPassword.Text);
            UserAccount userAcc = new UserAccount();
            userAcc.UserName = txbUserName.Text;
            userAcc.Password = sHashedPass;
            userAcc.UserTypeId = Int32.Parse(ddlUserTypes.SelectedItem.Value);
            if (userAcc.UserTypeId == 0)
            {
                userAcc.ClientId = ddlClientIds.SelectedValue;
            }
            userAcc.Active = chkbActive.Checked;
            UserAccountServices.Add(userAcc);
            Response.Redirect("~/UsersForm.aspx");
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if (Session["UserAccounts"] == null)
            {
                return;
            }
            List<UserAccount> original_uac = ((UserAccountCollection)Session["UserAccounts"]).ToList<UserAccount>();
            UserAccountCollection updated_uac = new UserAccountCollection();
            bool update = false;
            foreach (DataListItem item in dlUserAccounts.Items)
            {
                string username = dlUserAccounts.DataKeys[item.ItemIndex].ToString();
                UserAccount userAccount = new UserAccount(username);
                Predicate<UserAccount> findMatch = new Predicate<UserAccount>(userAccount.MatchFound);
                userAccount = original_uac.Find(findMatch);
                CheckBox chkbActive = (CheckBox)item.FindControl("chkActive");
                if (userAccount.Active != chkbActive.Checked)
                {
                    userAccount.Active = chkbActive.Checked;
                    updated_uac.Add(userAccount);
                    update = true;
                    
                }
            }
            Session["UserAccounts"] = null;
            if (update)
            {
                UserAccountServices.UpdateCollection(updated_uac);
                Response.Redirect("~/UsersForm.aspx");
            }
            
        }
    }
}
